package mobi.hami.huapin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.dao.NoteInfoMapper;
import mobi.hami.huapin.dao.NoteScheduleMapper;
import mobi.hami.huapin.dao.NoteSortMapper;
import mobi.hami.huapin.model.NoteInfo;
import mobi.hami.huapin.model.NoteSchedule;
import mobi.hami.huapin.model.NoteSort;
import mobi.hami.huapin.service.ManagementLaunchService;
import mobi.hami.huapin.util.Dates;
import mobi.hami.huapin.util.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ManagementLaunchServiceImpl implements ManagementLaunchService {

    private static final Logger logger = LoggerFactory.getLogger(ManagementLaunchServiceImpl.class);

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    NoteScheduleMapper noteScheduleMapper;

    @Autowired
    NoteSortMapper noteSortMapper;

    @Autowired
    NoteInfoMapper noteInfoMapper;



    /**
     * 某个纸条排期-上线
     * @param id 主键
     * @param order_num 坑号
     * @return
     */
    public Result onlineNoteSchedule(Integer id,Integer order_num){
        int m = noteScheduleMapper.updateNoteScheduleStatusByorderNum(order_num,1);
        NoteSchedule noteSchedule = noteScheduleMapper.selectByPrimaryKey(id);
        if(null != noteSchedule){
            noteSchedule.setStatus(0);
            noteSchedule.setStart_time(new Date());
            int n = noteScheduleMapper.updateByPrimaryKey(noteSchedule);
            if(n > 0){
                return  Results.successWithStatus(0,"操作成功");
            }
        }
        return Results.failureWithStatus(1,"操作失败");
    }


    /**
     * 某个纸条排期-下线
     * @param id 主键
     * @return
     */
    public Result offlineNoteSchedule(Integer id){
        int i = noteScheduleMapper.updateNoteScheduleStatus(id,1);
        if(i > 0){
            return  Results.successWithStatus(0,"操作成功");
        }
        return Results.failureWithStatus(1,"操作失败");
    }

    /**
     * 修改时间
     * @param id 主键
     * @param  start_time
     */
    public Result updateTime(Integer id,Date start_time){
        NoteSchedule noteSchedule = noteScheduleMapper.selectByPrimaryKey(id);
        if(null != noteSchedule){
            noteSchedule.setStart_time(start_time);
            int i = noteScheduleMapper.updateByPrimaryKey(noteSchedule);
            if(i > 0){
                return  Results.successWithStatus(0,"操作成功");
            }
        }
        return Results.failureWithStatus(1,"操作失败");
    }

    /**
     * 某个纸条排期-删除
     * @param id 主键
     * @return
     */
    public Result delete(Integer id){
        if(noteScheduleMapper.deleteByPrimaryKey(id) > 0){
            return  Results.successWithStatus(0,"操作成功");
        }
        return Results.failureWithStatus(1,"操作失败");
    }


    /**
     * 纸条排期-列表
     */
    public List<NoteSchedule> selectWitchParam(Map<String,Object> mm){
        return noteScheduleMapper.selectWitchParam(mm);
    }

    /**
     * 纸条分类列表
     */
    public Result noteSorts(){
        List<NoteSort>  list = noteSortMapper.select(null);
        return Results.successWithData(list);
    }


    /**
     * 纸条排期-纸条搜索
     */
    public List<NoteInfo> searchNotes(Map<String, Object> mm){
        List<NoteInfo> list = noteInfoMapper.selectNoteinfosWitchParam(mm);
        return list;
    }

    /**
     * 纸条排期-批量添加(无坑号无时间)-自动并入对应坑位并上线
     */
    public Result batchAddNoteschedules(NoteSchedule [] ms){
        if(null == ms || ms.length == 0 || ms.length >36){
            return Results.failureWithStatus(1,"最多0-36个");
        }

        if(null != ms && ms.length > 0 && ms.length <=36){
            List<NoteSchedule> list = new ArrayList<NoteSchedule>();
            for(int i = 0;i < ms.length; i++){
                //迭代号则默认为坑号
                int order_num = i+1;
                NoteSchedule noteSchedule = ms[i];
                //没时间直接上线
                NoteSchedule toSaveNoteSchedule = new NoteSchedule();
                toSaveNoteSchedule.setOrder_num(order_num);
                toSaveNoteSchedule.setNote_id(noteSchedule.getNote_id());
                toSaveNoteSchedule.setSort_id(noteSchedule.getSort_id());
                toSaveNoteSchedule.setContent(noteSchedule.getContent());
                toSaveNoteSchedule.setStatus(0);
                list.add(toSaveNoteSchedule);
                //删除该坑位没有时间的记录,保证该坑内最多只有一条无时间的上线记录
                noteScheduleMapper.deleteWithNoTimeLog(order_num);
                //更新该分类时间戳
                noteSortMapper.updateVersion(noteSchedule.getSort_id(),System.currentTimeMillis());
            }
            if(null != list && !list.isEmpty()){
                int n = noteScheduleMapper.batchAdd(list);
                if(n > 0){
                    return  Results.successWithStatus(0,"操作成功");
                }
            }
        }
        return Results.failureWithStatus(1,"操作失败");
    }

    /**
     * 纸条排期-批量添加纸条(某些有时间)
     */
    public Result addNoteschedules( NoteSchedule [] ms){
        if(null == ms || ms.length == 0 || ms.length >36){
            return Results.failureWithStatus(1,"最多0-36个");
        }
        for(int i = 0;i < ms.length; i++){
            NoteSchedule noteSchedule = ms[i];
            NoteSchedule tosaveObj = new NoteSchedule();
            tosaveObj.setNote_id(noteSchedule.getNote_id());
            tosaveObj.setSort_id(noteSchedule.getSort_id());
            tosaveObj.setContent(noteSchedule.getContent());
            tosaveObj.setOrder_num(noteSchedule.getOrder_num());
            if(null == noteSchedule.getStart_time() || noteSchedule.getStart_time().equals("")){
                tosaveObj.setStatus(0);
                tosaveObj.setStart_time(null);
                //删除该坑位没有时间的记录,保证该坑内最多只有一条无时间的上线记录
                noteScheduleMapper.deleteWithNoTimeLog(noteSchedule.getOrder_num());
            }else{
                tosaveObj.setStart_time(noteSchedule.getStart_time());
                tosaveObj.setStatus(1);
            }
            int m = noteScheduleMapper.insert(tosaveObj);
            if(m > 0){
                noteSortMapper.updateVersion(noteSchedule.getSort_id(),System.currentTimeMillis());
            }
        }
        return Results.successWithStatus(0,"成功");
    }



}
